<?php
namespace App\Repository\Db;
use App\Repository\Contracts\RoleInterface;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-16 10:07
 *
 * @Description:
 */
class RoleRepository extends Repository implements RoleInterface
{
    protected $rules = [
        'created' => [
            'name' => 'required|min:2|max:16|unique:role,name',
            'description' => 'required|min:2|max:255',
        ],
        'updated' => [
            'name' => 'required|min:2|max:16|unique:role',
            'description' => 'required|min:2|max:255'
        ]
    ];

    protected $messages = [
        'name.required' => "角色名不能为空",
        'name.min' => "角色名不能少于2个字",
        'name.max' => "角色名不能多于16个字",
        'name.unique' => "角色名不能重复",
        'description.required' => "角色描述不能为空",
        'description.min' => "角色描述不能少于2个汉字",
        'description.max' => "角色描述不能多于255个汉字"
    ];

    protected function modelName()
    {
        return 'App\Model\Role';
    }

    public function lists()
    {
        return $this->model->where('id', 2)->get();
    }

    public function detail($id)
    {
        return $this->find($id);
    }

    public function created(array $data)
    {
        // 表单验证
        $this->validate($data, 'created');

        return $this->save($data);
    }

    public function updated($id, array $data)
    {
        // 表单验证
        $this->validate($data, 'updated', ['name' => $id]);

        return $this->update($data, $id, 'id');
    }
}